package com.xyh.reflex;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Persion implements Serializable {
		   private String name;
		   private int age;
   
		public String getName() {
			return name;
		}
		
		public void setName(String name) {
			this.name = name;
		}
		
		public int getAge() {
			return age;
		}
		
		public void setAge(int age) {
			this.age = age;
		}

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
        Persion persion=new Persion();
        persion.setName("xiaoming");
        persion.setAge(23);
        Class clazz=persion.getClass();
        Field[] fields=clazz.getDeclaredFields(); 
        for(Field field:fields) {
        	String key=field.getName();
        	PropertyDescriptor descriptor=new PropertyDescriptor(key, clazz);
        	   Method method = descriptor.getReadMethod();
               Object value = method.invoke(persion);

               System.out.println(key + ":" + value);
        }
        
	}

}
